<template>
  <div class="city">
    <div class="search_wrap">
      <div class="search">
        <i class="fa fa-search"></i>
        <input v-model="city_val" type="text" placeholder="请输入城市名" />
      </div>
      <button @click="$router.push({name:'address',params:{city:city}})">取消</button>
    </div>
    <div style="height:100%"   v-if="searchList.length ==0">
      <div class="location">
        <Location @click="selectCity({name:city})" :address="city" />
      </div>
      <Alphabet @selectCity="selectCity" ref="allcity" :cityInfo="cityInfo" :keys="keys" />
    </div>
    <div class="search_list"  v-else>
      <ul>
        <li @click="selectCity(item)" v-for="(item,index) in searchList" :key="index">{{item.name}}</li>
      </ul>
    </div>
  </div>
</template>
<script>
import Location from "../components/Location";
import Alphabet from "../components/Alphabet";
import Toast from "vue-toast-mobile";
export default {
  name: "City",
  data() {
    return {
      city_val: "",
      cityInfo: {},
      keys: [],
      allCities: [],
      searchList: []
    };
  },
  // 计算属性，一般用来获取内存或者vuex 里面储存的数据
  computed: {
    city() {
      return (
        this.$store.getters.location.addressComponent.city ||
        this.$store.getters.location.addressComponent.province
      );
    }
  },
  //调接口 不能在这里操作dom  $el 还没有挂载 ，数据已经存在这个生命周期了
  created() {
    this.getCityInfo();
  },
  watch: {
    city_val() {
      // console.log(this.city_val);
      this.searchCity();
    }
  },
  components: {
    Location,
    Alphabet
  },
  methods: {
    selectCity(city) {
      //页面跳转
      this.$router.push({ name: "address", params: { city: city.name } });
    },
    getCityInfo() {
      //在请求前loading
      // const index = this.$layer.open({
      //   type: 2,
      //   content: "加载中..."
      // });
      this.$axios.defaults.headers["Content-Type"] =
        "application/x-www-form-urlencoded;charset=UTF-8"; //此处是增加的代码，设置请求头的类型
      this.$axios("/api/posts/cities")
        .then(res => {
          //  console.log(res.data);
          this.cityInfo = res.data;
          // 关闭层,请求成功，关闭loaing
          // this.$layer.close(index);
          this.$layer.msg("请求成功", () => console.log("end!!!"));
          //(23) ["S", "H", "N", "G", "X", "T", "Q", "W", "C", "F", "Y", "P", "J", "L", "Z", "B", "D", "K", "M", "R", "A", "E", "hotCities", __ob__: Observer]
          this.keys = Object.keys(res.data); //获取对象的所有的 keys
          //  console.log(this.keys)
          //移除数组的最后一个key
          this.keys.pop();
          // keys 排序
          this.keys.sort();
          //请求接口拿到数据渲染到页面上是一个异步的过程，created  生命周期是dom 还没有进行渲染，使用nextTick会在dom 渲染后回调这个方法
          //initScroll 由 Alphabet 传入
          this.$nextTick(() => {
            this.$refs.allcity.initScroll();
          });
          //先遍历外层得到 key ,然后遍历里层 ，传入key ,得到value
          this.keys.map(key => {
            console.log(key);
            this.cityInfo[key].forEach(element => {
              console.log(element);
              //获取所有的城市
              this.allCities.push(element);
            });
          });
        })
        .catch(err => {
          console.log(err);
          this.$layer.msg("请求失败", () => console.log("end!!!"));
        });
    },
    searchCity() {
      //如果搜索框为空，searchList为空
      //indexOf()； 返回值 -1  为-1没有找到 反之找到了
      if (!this.city_val) {
        this.searchList = [];
      } else {
        //使用filter  函数进行过滤，有一个返回值赋值给 this.searchList
          this.searchList = this.allCities.filter(item => {
          return item.name.indexOf(this.city_val) != -1;
        });
        console.log(this.searchList);
      }
    }
  }
};
</script>
<style scoped>
.city {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  overflow: auto;
  padding-top: 45px;
}
.city .search_wrap {
  background: #fff;
  /* flex 布局   space-between 让里面的两个元素间隔开*/
  display: flex;
  justify-content: space-between;
  /* 外边距 */
  padding: 3px 16px;
  /* fixed 会根据上下左右的位置确定距离 */
  position: fixed;
  top: 0;
  height: 45px;
  width: 100%;
  box-sizing: border-box;
}
.search_wrap .search {
  background-color: #eeeeee;
  /* height: 45px; */
  line-height: 40px;
  border-radius: 10px;
  box-sizing: border-box;
  padding: 0 16px;
  width: 85%;
}
.search_wrap .search input {
  background: #eee;
  outline: none;
  border: none;
  margin-left: 5px;
}
.search_wrap button {
  outline: none;
  color: #009eef;
  border: none;
  background-color: #fff;
}

.location {
  background: #fff;
  padding: 8px 16px;
  height: 65px;
  box-sizing: border-box;
}

.search_list {
  background: #fff;
  padding: 5px 16px;
}
.search_list li {
  padding: 10px;
  border-bottom: 1px solid #eee;
}
</style>


